package group.rober.jeki.controller

import group.rober.jeki.entity.Article
import group.rober.jeki.entity.Domain
import group.rober.jeki.entity.Tag
import group.rober.jeki.service.ArticleService
import group.rober.jeki.service.CommentService
import group.rober.jeki.service.DomainService
import group.rober.jeki.service.TagService
import group.rober.saber.kit.ClassKit
import group.rober.saber.kit.MapKit
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.servlet.ModelAndView

import javax.servlet.http.HttpServletRequest

@Controller
class IndexController {

    @Autowired
    DomainService domainService;
    @Autowired
    ArticleService articleService;
    @Autowired
    CommentService commentService;
    @Autowired
    TagService tagService;
    private int portalSize = 7;

    @RequestMapping("/")
    ModelAndView index(HttpServletRequest request) {

        Stack<ClassLoader> stack = ClassKit.getClassLoaderStack(this.class);
        String indent = "";
        while(stack.size()>0){
            ClassLoader classLoader = stack.pop();
            System.out.println(indent+"\\_"+classLoader.getClass().getName());
            indent += "  ";
        }

        Map<String,Object> vars = new HashMap<String,Object>();
        vars.put("stickedArticleList",articleService.getStickedArticleList());          //推荐文章
        vars.put("recentArticleList",articleService.getRecentArticleList(portalSize));  //最新文章
        vars.put("tweetList",commentService.getTweetList());                            //动弹

        //[取标准文章]信息块
        List<Tag> officalDocTagList = getDomainTags("official-document");
        List<Tag> officalDocArticleList = articleService.getArticleListByDomain("official-document",portalSize);
        vars.put("officalDocTagList",officalDocTagList);                                //[标准文档]标签列表
        vars.put("officalDocArticleList",officalDocArticleList);                        //[标准文档]文章列表

        //[常见问题]信息块
        List<Tag> faqTagList = getDomainTags("faq");
        List<Tag> faqArticleList = articleService.getArticleListByDomain("faq",portalSize);        //[常见问题]信息块
        vars.put("faqTagList",faqTagList);                                //[常见问题]标签列表
        vars.put("faqArticleList",faqArticleList);                        //[常见问题]文章列表

        //[问答交流]
        List<Tag> snsTagList = getDomainTags("synthesize");
        List<Tag> snsArticleList = articleService.getArticleListByDomain("synthesize",portalSize);
        vars.put("snsTagList",snsTagList);                                //[问答交流]标签列表
        vars.put("snsArticleList",snsArticleList);                        //[问答交流]文章列表

        //[业务方案]
        List<Tag> bzsoTagList = getDomainTags("business-solution");
        List<Tag> bzsoArticleList = articleService.getArticleListByDomain("business-solution",portalSize);
        vars.put("bzsoTagList",bzsoTagList);
        vars.put("bzsoArticleList",bzsoArticleList);
        //所有标签
        vars.put("allTagList",tagService.getTagList());

        //[JAVA]
        List<Tag> javaTagList = getDomainTags("java");
        List<Tag> javaArticleList = articleService.getArticleListByDomain("java",portalSize);
        vars.put("javaTagList",javaTagList);
        vars.put("javaArticleList",javaArticleList);

        //[数据库]
        List<Tag> dbTagList = getDomainTags("database");
        List<Tag> dbArticleList = articleService.getArticleListByDomain("database",portalSize);
        vars.put("dbTagList",dbTagList);
        vars.put("dbArticleList",dbArticleList);

        return new ModelAndView("views/index",vars);
    }

    protected List<Tag> getDomainTags(String domainCode){
        Domain domain = domainService.getDomainByCode(domainCode);
        return domainService.getInDomainTagList(domain.getId(),3);
    }

    @RequestMapping("/domain/{domainCode}")
    ModelAndView domain(@PathVariable("domainCode") String domainCode,Map<String,Object> vars) {
        if(vars==null)vars = new HashMap<String,Object>();
        List<Article> articleList = null;

        Tag tag = (Tag)vars.get("activeTag");
        if(tag==null){
            tag = new Tag();
            vars.put("activeTag",tag);
        }else{
            articleList = articleService.getArticleListByTag(tag.getCode(),-1);
        }

        Domain domain = domainService.getDomainByCode(domainCode);
        if(domain!=null){
            if(articleList==null){
                articleList = articleService.getArticleListByDomain(domainCode,-1);
            }
            List<Tag> tagList = domainService.getInDomainTagList(domain.getId());
            vars.put("tagList",tagList);
        }
        vars.put("articleList",articleList);

        vars.put("activeDomain",domain);

        return new ModelAndView("views/domain",vars);
    }

    @RequestMapping("/domain-tag/{domainCode}/{tagCode}")
    ModelAndView domainTag(@PathVariable("domainCode") String domainCode,@PathVariable("tagCode") String tagCode) {
        Map<String,Object> vars = new HashMap<String,Object>();

        Tag tag = tagService.getTagByCode(tagCode);
        vars.put("activeTag",tag);
        return domain(domainCode,vars);
    }

    @RequestMapping("/tag/{tagCode}")
    ModelAndView tag(@PathVariable("tagCode") String tagCode) {
        Map<String,Object> vars = new HashMap<String,Object>();

        Tag tag = tagService.getTagByCode(tagCode);
        List<Article> articleList = articleService.getArticleListByTag(tag.getCode(),-1);;

        vars.put("activeTag",tag);
        vars.put("articleList",articleList);
        return new ModelAndView("views/tag",vars);
    }

    @RequestMapping("/register")
    ModelAndView register() {
        new ModelAndView("views/register",[:]);
    }

    @RequestMapping("/logon")
    ModelAndView logon() {
        new ModelAndView("views/logon",MapKit.newHashMap());
    }
    @RequestMapping("/logoff")
    ModelAndView logoff() {
        return index();
    }
    @RequestMapping("/logonAction")
    ModelAndView logonAction() {
        new ModelAndView("views/logon",[:]);
    }

    @RequestMapping("/markdown-guide")
    ModelAndView markdownGuide() {
        Map<String,Object> vars = MapKit.newHashMap();
        return new ModelAndView("views/markdown-guide",vars);
    }
//    @RequestMapping("/article/{articleId}")
//    ModelAndView article() {
//        new ModelAndView("views/article",[:]);
//    }
//
//    @RequestMapping("/article-create")
//    ModelAndView createArticle() {
//        new ModelAndView("views/article-editor",[:]);
//    }
//    @RequestMapping("/article-editor")
//    ModelAndView editArticle(@PathVariable("articleId") String articleId) {
//        new ModelAndView("views/article-editor",[:]);
//    }
    @RequestMapping("/redirector/*")
    ModelAndView redirector() {
        System.out.println("OK:");
        new ModelAndView("views/index",[:]);
    }
}
